package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcProjectSgazht;
import com.ruoyi.gcgl.domain.GcProjectSgazhtjgys;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtjgysService;

/**
 * 施工安装合同竣工验收Controller
 * 
 * @author ruoyi
 * @date 2022-12-11
 */
@Controller
@RequestMapping("/gcgl/sgazhtjgys")
public class GcProjectSgazhtjgysController extends BaseController
{
    private String prefix = "gcgl/sgazhtjgys";

    @Autowired
    private IGcProjectSgazhtjgysService gcProjectSgazhtjgysService;
    @Autowired
    private IGcProjectSgazhtService gcProjectSgazhtService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IGcProjectService gcProjectService;
    
    private static final String FILE_DELIMETER = ",";

    @RequiresPermissions("gcgl:sgazhtjgys:view")
    @GetMapping()
    public String sgazhtjgys()
    {
        return prefix + "/sgazhtjgys";
    }

    /**
     * 查询施工安装合同竣工验收列表
     */
    @RequiresPermissions("gcgl:sgazhtjgys:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectSgazhtjgys gcProjectSgazhtjgys)
    {
        startPage();
        gcProjectSgazhtjgys.setDeleteFlag(1);
        if(getSysUser().isShow43R(getUserId())){
        	gcProjectSgazhtjgys.setCreateUser(getLoginName());
        }
        List<GcProjectSgazhtjgys> list = gcProjectSgazhtjgysService.selectGcProjectSgazhtjgysList(gcProjectSgazhtjgys);
        
        for(GcProjectSgazhtjgys obj:list){
        	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(obj.getSgazhtId());
        	if(gcProjectSgazht!=null){
        		obj.setSupplyName(gcProjectSgazht.getSupplyName());
        	}
        }
        
        return getDataTable(list);
    }

    /**
     * 导出施工安装合同竣工验收列表
     */
    @RequiresPermissions("gcgl:sgazhtjgys:export")
    @Log(title = "施工安装合同竣工验收", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectSgazhtjgys gcProjectSgazhtjgys)
    {
    	gcProjectSgazhtjgys.setDeleteFlag(1);
        if(getSysUser().isShow()){
        gcProjectSgazhtjgys.setCreateUser(getLoginName());
        }
        List<GcProjectSgazhtjgys> list = gcProjectSgazhtjgysService.selectGcProjectSgazhtjgysList(gcProjectSgazhtjgys);
        ExcelUtil<GcProjectSgazhtjgys> util = new ExcelUtil<GcProjectSgazhtjgys>(GcProjectSgazhtjgys.class);
        return util.exportExcel(list, "施工安装合同竣工验收数据");
    }

    /**
     * 新增施工安装合同竣工验收
     */
    @GetMapping("/add/{sgazhtId}")
    public String add(@PathVariable("sgazhtId") Long sgazhtId, ModelMap mmap){
    	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(sgazhtId);
    	mmap.put("gcProjectSgazht", gcProjectSgazht);
    	
    	mmap.put("gcProjectid", gcProjectSgazht.getGcProjectid());
    	mmap.put("sgazhtId", sgazhtId);
    	mmap.put("orderNo", gcProjectSgazht.getOrderNo());
    	
    	GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectSgazht.getGcProjectid());
    	mmap.put("gcProject", gcProject);
        return prefix + "/add";
    }

    /**
     * 新增保存施工安装合同竣工验收
     */
    @RequiresPermissions("gcgl:sgazhtjgys:add")
    @Log(title = "施工安装合同竣工验收", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcProjectSgazhtjgys gcProjectSgazhtjgys){
    	try{
            gcProjectSgazhtjgys.setDeleteFlag(1);
            gcProjectSgazhtjgys.setCreateAt(new Date());
            gcProjectSgazhtjgys.setUpdateAt(new Date());
            gcProjectSgazhtjgys.setCreateUser(getLoginName());
            gcProjectSgazhtjgys.setUpdateUser(getLoginName());
            gcProjectSgazhtjgysService.insertGcProjectSgazhtjgys(gcProjectSgazhtjgys);
    	}catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 修改施工安装合同竣工验收
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazhtjgys obj = gcProjectSgazhtjgysService.selectGcProjectSgazhtjgysById(id);
        if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
        	String[] zlis = obj.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	obj.setZilwj(file);
        }
        mmap.put("gcProjectSgazhtjgys", obj);
        
        GcProjectSgazht gcProjectSgazht = 
        		gcProjectSgazhtService.selectGcProjectSgazhtById(obj.getSgazhtId());
        mmap.put("gcProjectSgazht", gcProjectSgazht);
        
        GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectSgazht.getGcProjectid());
    	mmap.put("gcProject", gcProject);
        return prefix + "/edit";
    }
    
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazhtjgys obj = gcProjectSgazhtjgysService.selectGcProjectSgazhtjgysById(id);
        if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
        	String[] zlis = obj.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	obj.setZilwj(file);
        }
        mmap.put("gcProjectSgazhtjgys", obj);
        
        GcProjectSgazht gcProjectSgazht = 
        		gcProjectSgazhtService.selectGcProjectSgazhtById(obj.getSgazhtId());
        mmap.put("gcProjectSgazht", gcProjectSgazht);
        
        GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectSgazht.getGcProjectid());
    	mmap.put("gcProject", gcProject);
        return prefix + "/detail";
    }

    /**
     * 修改保存施工安装合同竣工验收
     */
    @Log(title = "施工安装合同竣工验收", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectSgazhtjgys gcProjectSgazhtjgys)
    {
    	try{
            gcProjectSgazhtjgys.setUpdateAt(new Date());
            gcProjectSgazhtjgys.setUpdateUser(getLoginName());
            gcProjectSgazhtjgysService.updateGcProjectSgazhtjgys(gcProjectSgazhtjgys);
        }
        catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 删除施工安装合同竣工验收
     */
    @RequiresPermissions("gcgl:sgazhtjgys:remove")
    @Log(title = "施工安装合同竣工验收", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectSgazhtjgysService.deleteGcProjectSgazhtjgysByIds(ids));
    }
    
    /**
     * 通用上传请求（多个）
     */
    @PostMapping("/uploads")
    @ResponseBody
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception{
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            List<String> urls = new ArrayList<String>();
            List<String> fileNames = new ArrayList<String>();
            List<String> newFileNames = new ArrayList<String>();
            List<String> originalFilenames = new ArrayList<String>();
            for (MultipartFile file : files)
            {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                urls.add(url);
                fileNames.add(fileName);
                newFileNames.add(FileUtils.getName(fileName));
                originalFilenames.add(file.getOriginalFilename());
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
            ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
            ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}